package site.shihuan.helpdesk.service.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.shihuan.helpdesk.common.model.JwtUser;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;
import site.shihuan.helpdesk.security.context.UserContext;
import site.shihuan.helpdesk.service.exception.ServiceResultCode;
import site.shihuan.helpdesk.service.manager.ContactManager;
import site.shihuan.helpdesk.service.manager.VisitorManager;
import site.shihuan.helpdesk.service.model.vo.ClerkVO;
import site.shihuan.helpdesk.service.model.vo.VisitorVO;

import java.util.List;

/**
 * 客服API
 *
 * @author 周世焕
 * @Date 2021/3/1 21:58
 */
@RestController
@RequestMapping("/v1/service/contact")
public class ContactController {

    @Autowired
    private VisitorManager visitorManager;

    @Autowired
    private ContactManager contactManager;

    /**
     * 查询当前该访客一对一联系的客服
     * @param visitorId
     */
    @GetMapping("customer-service")
    public RestResult findMyCustomerService(String visitorId,String tenantId) {
        ClerkVO clerkVO = visitorManager.findMyDeskClerk(visitorId,tenantId);
        if(clerkVO != null) {
            return RestResultUtils.success(clerkVO);
        }
        return RestResultUtils.failedWithMsg(ServiceResultCode.WAITING);
    }

    /**
     * 查询当前联系人列表
     * @return
     */
    @GetMapping("current")
    public RestResult findCurrentContacts() {
        JwtUser user = UserContext.getUser();
        List<VisitorVO> contacts= contactManager.findContacts(user.getUid(),user.getTenantId());
        return RestResultUtils.success(contacts);
    }

    /**
     * 接入访客
     * @param visitorId
     * @return
     */
    @PostMapping("accept")
    public RestResult acceptVisitor(String visitorId){
        JwtUser user = UserContext.getUser();
        return RestResultUtils.success(contactManager.acceptVisitor(user.getUid(),visitorId,user.getTenantId()));
    }

    /**
     * 结束客服
     * @param contactId
     * @return
     */
    @PostMapping("finish")
    public RestResult finishContact(String contactId){
        contactManager.finishContact(contactId);
        return RestResultUtils.success();
    }
}
